package com.iking.housekeeping.util.archives;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ch.qos.logback.core.net.SyslogOutputStream;
import org.apache.commons.collections4.MapUtils;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import com.iking.housekeeping.service.user.LoginService;

/**
 * 拦截器
 * @author 张重阳
 * @date 2018年2月27日15:51:34
 * @version 1.0
 */
@Aspect
@Component
public class HttpBearerAuthorizeAttribute implements Filter {

	@Autowired
	private LoginService loginService;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

		SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, filterConfig.getServletContext());

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		// 登录和测试接口放行，其余接口全部监听
		//swagger接口
		boolean isSwagger = swaggerURLTest(httpRequest);
		//登录接口
		boolean isLogin = loginURLTest(httpRequest);
		//移动端接口
		boolean isMobile = mobileURLTest(httpRequest);
		//下载接口
		boolean down=down(httpRequest);
		//上传图片接口
		boolean uploadimage=uploadimage(httpRequest);
		//服务器登录接口过滤路径
		boolean htmls = httpRequest.getRequestURI().equals("/");
		boolean css=httpRequest.getRequestURI().endsWith("/index.css");
		boolean indext=httpRequest.getRequestURI().endsWith("/index.js");
		boolean font=httpRequest.getRequestURI().endsWith("/font_148784_v4ggb6wrjmkotj4i.woff");
		boolean img=httpRequest.getRequestURI().endsWith("/static/login2.ca9b5212.png");
		boolean log=httpRequest.getRequestURI().endsWith("/static/logo.541334e5.png");
		boolean favicon=httpRequest.getRequestURI().endsWith("/favicon.ico");
		if (isLogin || isSwagger || isMobile || down ||uploadimage || htmls|| indext || font ||img||css||log||favicon) {
			chain.doFilter(request, response);
			return;
		}
		// 从请求header中拿出webtoken
		String webtoken = httpRequest.getHeader("accessToken");
		Integer webtokenLength = 7;
		if ((webtoken != null) && (webtoken.length() > webtokenLength)) {
			if (loginService.loginCheck(webtoken))
			{
				chain.doFilter(request, response);
				return;
			}
		}
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		httpResponse.setCharacterEncoding("UTF-8");
		httpResponse.setContentType("application/json; charset=utf-8");
		httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
		return;
	}
	
	/**
	 * 判断是否登录接口
	 * @param httpRequest
	 * @return
	 */
	private Boolean loginURLTest(HttpServletRequest httpRequest) {
		return httpRequest.getRequestURL().toString().endsWith("login");
	}
	
	/**
	 * 判断是否swagger接口
	 * @return
	 */
	private Boolean swaggerURLTest(HttpServletRequest httpRequest) {
		boolean isSwagger = httpRequest.getRequestURL().toString().contains("swagger");
		boolean isSwaggerApi = httpRequest.getRequestURL().toString().contains("api-docs");
		if(isSwagger || isSwaggerApi) {
			return true;
		}
		return false;
	}
	
	/**
	 * 判断是否移动端接口
	 * @param httpRequest
	 * @return
	 */
	private Boolean mobileURLTest(HttpServletRequest httpRequest) {
		int i=httpRequest.getRequestURL().toString().indexOf("/login/servicestaff");
		if(i!=-1){
			return  true;
		}
		return false;
	}

	@Override
	public void destroy() {
		
	}

	/**
	 * 判断是否删除服务人员接口
	 * @param httpRequest
	 * @return
	 */
	private Boolean deletStaff(HttpServletRequest httpRequest) {
		return httpRequest.getRequestURL().toString().endsWith("deleteBatch");
	}
	private Boolean down(HttpServletRequest httpRequest) {
		int i=httpRequest.getRequestURL().toString().indexOf("downloadFile");
         if(i!=-1){
         	return  true;
		 }
		 return false;
	}

	private Boolean uploadimage(HttpServletRequest httpRequest) {
		return httpRequest.getRequestURL().toString().endsWith("uploadimage");
	}
}
